package org.gruposp2p.proyectokarma.controller

import org.gruposp2p.cometd.KarmaMessageService;
import org.gruposp2p.cometd.KarmaMessageEvent;
import org.gruposp2p.proyectokarma.domain.Peer;
import org.gruposp2p.proyectokarma.domain.Document;
import org.gruposp2p.proyectokarma.domain.Vote;
import org.gruposp2p.proyectokarma.domain.CometdMessage;
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import grails.plugins.springsecurity.Secured

class PruebaController {

	def springSecurityService
    KarmaMessageService karmaMessageService;
    def eventBroker;

    def index = {
        def result = karmaMessageService.sendMessageToPeer(params.message, params.area, params.peerId);
        render "Result: " + result + "<br/>" + " - Message: " + params.message + 
            " - area: " + params.area + " - peerId: " + params.peerId
    }


    
    def prueba = {
        for (int i=0; i<50; i++) {
            def username = 'Peer' + i
            def	enabled = true
            def email = username + '@spain.es'
            def dateOfBird = new Date();
            def peer = new Peer(username: username, enabled: enabled, email:email,
                dateOfBird:dateOfBird, authority:"ROLE_USER", dateAdded:dateOfBird,
                name:username)
            private Set documents = new HashSet(0);
            for (int j=0; j<2; j++) {
                    String name = 'Nombre del documento: \'' + i + j + '\'';
                    String content = 'Contenido del documento ' + i + j;
                    def document = new Document (name:name, content:content);
                    document.setPeer(peer);
                    documents.add(document);
            }
            peer.setDocuments(documents);
            peer.save(flush: true)
        }
    }

	@Secured(['ROLE_USER'])
	def index1 = {
		def principal = springSecurityService.principal
		String username = principal.username
		def authorities = principal.authorities // a Collection of GrantedAuthority
		boolean enabled = principal.enabled
		log.debug " - principal: " + principal + " - username: " + username + " - authorities: " + authorities + " - enabled: " + enabled
		render " - principal: " + principal + " - username: " + username + " - authorities: " + authorities + " - enabled: " + enabled
	}
	
	def prue = {
		log.debug grailsApplication.getClassForName(conf.userLookup.authorityJoinClassName)
		render "grailsApplication.getClassForName(conf.userLookup.authorityJoinClassName): " + grailsApplication.getClassForName(conf.userLookup.authorityJoinClassName)
    }
	
	protected String lookupUserRoleClassName() {
		SpringSecurityUtils.securityConfig.userLookup.authorityJoinClassName
	}

	protected Class<?> lookupUserRoleClass() {
		grailsApplication.getDomainClass(lookupUserRoleClassName()).clazz
	}
	
}
